package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.LeaderResponse;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysUser;
import com.juzi.wopu.domain.SysUserRole;
import com.juzi.wopu.query.LeaderQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysUserQuery;
import com.juzi.wopu.query.SysUserRoleQuery;
import com.juzi.wopu.service.ILeaderService;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysUserRoleService;
import com.juzi.wopu.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;

public class LeaderAction extends CRUDAaction<LeaderResponse>{
	
	private static final long serialVersionUID = 1L;
	
	private Long userId;
	
	private Long companyId;
	
	private Long userName;
	
	private String mobile;

	private ILeaderService leaderService;
	
	private ISysCodeService sysCodeService;
	
	private ISysUserService sysUserService;
	
	private ISysUserRoleService sysUserRoleService;
	
	private LeaderQuery baseQuery=new LeaderQuery();
	
	private PageList pageList;
	
	private LeaderResponse leaderResponse=new LeaderResponse();
	
	public LeaderResponse getLeaderResponse() {
		return leaderResponse;
	}
	
	public ISysUserRoleService getSysUserRoleService() {
		return sysUserRoleService;
	}

	public void setSysUserRoleService(ISysUserRoleService sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}

	public void setLeaderResponse(LeaderResponse leaderResponse) {
		this.leaderResponse = leaderResponse;
	}

	public ISysUserService getSysUserService() {
		return sysUserService;
	}

	public void setSysUserService(ISysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}

	public ISysCodeService getSysCodeService() {
		return sysCodeService;
	}

	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}

	public ILeaderService getLeaderService() {
		return leaderService;
	}

	public void setLeaderService(ILeaderService leaderService) {
		this.leaderService = leaderService;
	}

	public LeaderQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(LeaderQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	
	public Long getUserName() {
		return userName;
	}

	public void setUserName(Long userName) {
		this.userName = userName;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	@Override
	public LeaderResponse getModel() {
		return leaderResponse;
	}

	@Override
	protected void list() {
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		baseQuery.setCompanyName(companyName);
		this.pageList=leaderService.findByQuery(baseQuery);		
	}

	@Override
	public String save() throws Exception {
		Map<String, SysCode> map=new HashMap<>();
		Map<Long, SysCode> map1=new HashMap<>();
		sysCodeService.getSysCodeMap(map1, map);
		
		companyName=(String) ActionContext.getContext().getSession().get("companyName");
		long companyId=map.get(companyName).getiId();
		
		SysUserQuery query=new SysUserQuery();
		query.setiCompany(companyId);
		query.setsMobile(leaderResponse.getMobile());
		SysUser user=sysUserService.findOne(query);
		long userId=-1L;
		if(user==null){
			SysUserQuery query1=new SysUserQuery();
			query1.setsMobile(leaderResponse.getMobile());
			SysUser user1=sysUserService.findOne(query1);
			if(user1!=null){
				System.out.println("此用户在其他局点已经存在");
				return null;
			}
			else {
				//如果设置为领导的用户不存在，要先添加
				baseQuery.setCurrentPage(Integer.MAX_VALUE);
				SysUser sysUser=new SysUser();
				sysUser.setsUserNo(leaderResponse.getUserName());
				sysUser.setsMobile(leaderResponse.getMobile());
				sysUser.setDtInsert(new Date());
				sysUser.setiType(0L);
				sysUser.setiStatus(0L);
				sysUser.setsPass("123AbCdE");
				sysUser.setsSex("保密");
				sysUser.setiCompany(companyId);
				sysUserService.save(sysUser);
				userId=sysUserService.findOne(query).getiId();
			}
		}else {
			userId=user.getiId();
		}
		
		SysUserRoleQuery sysUserRoleQuery=new SysUserRoleQuery();
		sysUserRoleQuery.setiUserId(userId);
		sysUserRoleQuery.setiCompany(companyId);
		sysUserRoleQuery.setiRole(19L);
		PageList pList = sysUserRoleService.findByQuery(sysUserRoleQuery);
		if(pList!=null && pList.getRows()!=null && pList.getRows().size()>0){
			//如果查询到，就不再添加
		}else {
			SysUserRole sysUserRole=new SysUserRole();
			sysUserRole.setiUserId(userId);
			sysUserRole.setiRole(19L);
			sysUserRole.setDtInsert(new Date());
			sysUserRole.setiCreateUser(1L);
			sysUserRole.setiCompany(companyId);
			sysUserRoleService.save(sysUserRole);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (companyId != null && userId!=null && mobile!=null) {
				//sysUserService.deleteByMobile(mobile);//删除sysUser
				
				SysUserRoleQuery query=new SysUserRoleQuery();
				query.setiCompany(companyId);
				query.setiRole(19L);
				query.setiUserId(userId);
				sysUserRoleService.deleteByQuery(query);//删除sysUserRole
				
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			} else {
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:" + e.getMessage() + "\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
	}

	@Override
	public void prepareSave() throws Exception {
	}

}
